package com.ayf.payment.game.api.utils;

import com.swwx.charm.commons.lang.utils.LogPortal;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author sunyufei@lakala.com
 * @Title: URLCoderUtil
 * @Copyright: Copyright (c) 2018
 * @Description: <br> URL编码和解码类
 * @Company: lakala.com
 * @Created on 2018/7/2上午11:28
 */
public class URLCoderUtil {


    public final static String CHARSET_UTF8 = "UTF-8";



    public static String decode(String str,String charSet) throws UnsupportedEncodingException {
        return URLDecoder.decode(str,charSet);
    }

    public static String decode(String str) throws UnsupportedEncodingException {
        return decode(str,CHARSET_UTF8);
    }


    public static String encode(String str,String charSet) throws UnsupportedEncodingException {
        return URLEncoder.encode(str,charSet);
    }

    public static String encode(String str) throws UnsupportedEncodingException {
        return encode(str,CHARSET_UTF8);
    }

    public static String decodeNoException(String str,String charSet) {
        try {
            return decode(str,charSet);
        } catch (UnsupportedEncodingException e) {
            LogPortal.error("decodeNoException throw UnsupportedEncodingException",e);
            return "";
        }
    }

    public static String decodeNoException(String str) {
        return decodeNoException(str,CHARSET_UTF8);
    }

    public static String encodeNoException(String str,String charSet) {
        try {
            return encode(str,charSet);
        } catch (UnsupportedEncodingException e) {
            LogPortal.error("encodeNoException throw UnsupportedEncodingException",e);
            return "";
        }
    }

    public static String encodeNoException(String str) {
        return encodeNoException(str,CHARSET_UTF8);
    }
}
